package com.zls.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zls.mapper.ProductBatchesMapper;
import com.zls.mapper.TFarmMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.ProductBatches;
import com.zls.pojo.TFarm;
import com.zls.pojo.TTask;
import com.zls.service.ProductBatchesService;
import com.zls.service.TFarmService;
import com.zls.service.TTaskService;
import com.zls.vo.ProTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProductBatchesServiceImpl extends ServiceImpl<ProductBatchesMapper, ProductBatches> implements ProductBatchesService {
    @Autowired
    private ProductBatchesMapper productBatchesMapper;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<ProductBatches> page = productBatchesMapper.findPage((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = productBatchesMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Autowired
    private TFarmService tFarmService;
    @Autowired
    private TTask tTask;
    @Autowired
    private TTaskService tTaskService;
    @Override
    @Transactional
    public int addPro(ProTaskVo proTaskVo) {

        TFarm tFarm1 = tFarmService.selectId(proTaskVo.getFarmId());
        proTaskVo.setSysUserId(tFarm1.getSysUser().getId());
        int insert = productBatchesMapper.insert(proTaskVo);
        System.out.println("id:"+proTaskVo.getId());
        //给农事赋值
        List<TTask> farmingList = proTaskVo.getFarmingList();
        for (TTask task : farmingList) {
            tTask.setContent(task.getContent());
            TFarm tFarm = tFarmService.selectId(proTaskVo.getFarmId());
            tTask.setSysUserId(tFarm.getUserId());
            tTask.setFarmId(proTaskVo.getFarmId());
            tTask.setArea(proTaskVo.getArea()+"");
//            ProductBatches productBatches = selectByBatch(proTaskVo.getProductBatch());
            tTask.setProductBatchesId(proTaskVo.getId());
            System.out.println(tTask.getProductBatchesId());
            int i = tTaskService.addTTask(tTask);

        }
        return insert;
    }

    @Override
    @Transactional
    public int editPro(ProductBatches productBatches) {
        TFarm tFarm = tFarmService.selectId(productBatches.getFarmId());
        productBatches.setSysUserId(tFarm.getSysUser().getId());
        int i = productBatchesMapper.updateById(productBatches);
        return i;
    }

    @Override
    public ProductBatches selectByBatch(String productBatch) {
        return productBatchesMapper.selectByBatch(productBatch);
    }
}
